﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QA_System
{
    static class StringNumber
    {
        public static string ConvertToSound(string number)
        {
            string sound = "";                      
            string[] nums = number.Split(' ');
            nums[0] = ClearNumber(nums[0]);          
            string[] numPart = nums[0].Split('.');
            sound += ConvertIntegerNumber(numPart[0]);
            sound = sound.Trim();
            if (numPart.Length > 1)
            {
                if ( sound == "")
                    sound += "không";
                sound += " ";
                sound += "phẩy";
                sound += " ";
                sound += ConvertIntegerNumber(numPart[1]);
                sound = sound.Trim();
            }
            sound += " ";
            if (nums.Length > 1)
            {
                sound += DonviDo(nums[1]);
            }
            return sound;
        }

        //xoa so 0 o dau va cuoi so
        public static string ClearNumber(string number)
        {
            string temp;
            while (number[0] == '0')
            {
                number = number.Remove(0, 1);
            }
            if (number.Contains("."))
            {
                while (number[number.Length - 1] == '0')
                {
                    number = number.Remove(number.Length -1, 1);
                }
            }
            temp = number;
            return temp;
        }

        //chuyen chu so thanh tieng
        private static string ConvertNumberLetter(char number, int index, bool isOnlylLetter)
        {
            string result = "";
            switch (number)
	        {
                case '0' : 
                    if (index != 0)
                        result = "không";
                    break;
                case '1' :
                    if (!isOnlylLetter)
                    {
                        if (index == 0)
                            result = "mốt";
                        else if (index == 1)
                            result = "mười";
                        else
                            result = "một";
                    }
                    else
                        result = "một";                    
                    break;
                case '2' : 
                    result = "hai";
                    break;
                case '3' : 
                    result = "ba";
                    break;
                case '4' : 
                    result = "bốn";
                    break;
                case '5' : 
                    result = "năm";
                    break;
                case '6' : 
                    result = "sáu";
                    break;
                case '7' : 
                    result = "bảy";
                    break;
                case '8' : 
                    result = "tám";
                    break;
                case '9' : 
                    result = "chín";
                    break; 
	        }
            return result;
        }

        //chuyển đơn vị thành chữ
        private static string ConvertUnit(int index)
        {
            string result = "";
            switch (index)
            {
                case 0 :
                    break;
                case 1:
                    result = "mươi";
                    break;
                case 2 :
                    result = "trăm";
                    break;
                case 3:
                    result = "nghìn";
                    break;                
                default:
                    break;
            }
            return result;
        }

        //chuyen so nguyen sang tieng: ket hop chu so va don vi
        private static string ConvertIntegerNumber(string number)
        {
            
            string result = "";
            
            if (number.Length > 1)
                for (int i = 0; i < number.Length; i++)
                {
                    int index = number.Length - (i + 1);
                    result += ConvertNumberLetter(number[i], number.Length - (i + 1), false);
                    result += " ";
                    if ((number[i] != '1') || (index != 1))
                    {
                        result += ConvertUnit(index);
                        result += " ";
                    }
                }
            else
                result += ConvertNumberLetter(number[0], 0, true);
            return result;
        }
        //chuyen don vi do thanh tieng
        private static string DonviDo(string donVi)
        {
            string result;
            switch (donVi)
            {
                case "cm" :
                    result = "centimet";
                    break;
                case "mm" :
                    result = "minimet";
                    break;
                case "m" :
                    result = "met";
                    break;
                default:
                    result = donVi;
                    break;
            }
            return result;
        }


    }
}
